/////////////////////////////////////////////////////////////////////////
////    Data Storage Control - SQLite
////	By TGP1994
/////////////////////////////////////////////////////////////////////////

function initialDataCheck()
	if sql.TableExists("earthrp") then
		print("The table earthrp exists within the SQLite database.")
		return true
	end
	local result = sql.Query("CREATE TABLE earthrp (steamid text, rpname text, money integer)")
	print("Table earthrp created sucessfully.")
end
hook.Add("Initialize", "gamemodeInitializing", initialDataCheck)

function playerLoaded(ply, stid, unid)
	money = sql.QueryValue("SELECT money FROM earthrp WHERE steamid = '"..stid.."'")
	if money == nil then money = 500 end
	Msg("Got "..money.." from the Query.\n")
	ply:SetNWString("sid", stid)
	ply:SetNWInt("money", money)
	print("Sucessfully loaded settings for "..ply:Name())
end
hook.Add("PlayerAuthed", "plyrauthed", playerLoaded)

function playerLeaving(ply)
	updatedstuph = sql.QueryValue("UPDATE earthrp SET money = "..ply:GetNetworkedInt("money").." WHERE steamid = "..ply:SteamID())
	Msg("Updated player stats, query is returned as: "..tostring(updatedstuph)..".\n")
end
hook.Add("PlayerDisconnected", "plydiscon", playerLeaving)

function changeMoolah(ply, cmd, args)
	local moolah = args
	if ply == nil then
		print("Null player supplied to changeMoolah.")
		return
	end
	if moolah == nil or moolah[1] == 0 then
		print("Null or 0 amount of moolah supplied for changeMoolah.")
		return
	end
	local origmoolah = ply:GetNWInt("money")
	local newmoolah = origmoolah + moolah[1]
	ply:SetNWInt("money", newmoolah)
	print("Money updated sucessfully. Was: "..origmoolah.." Now is: "..newmoolah, 3)
end
concommand.Add("erp_changemoney", changeMoolah)